package observable

import observable.emitter.EmitterFactory
import observable.emitter.ObservableOnSubscribe
import observer.Observer

/**
 * 实现生成一个被观察者
 */
class ObservableCreate<T>(private val observableOnSubscribe: ObservableOnSubscribe<T>) : ObservableProxy<T>() {

    override fun subscribeProxy(observer: Observer<T>) {
        // 观察者与被观察者建立关系，外部通过发射器向被观察者发送消息
        dispatchSubscribe {//上流
            observableOnSubscribe.subscribe(EmitterFactory.create(observer, this))
        }
        // 回调下订阅成功函数
        dispatchObserve {//下流
            observer.onSubscribe()
        }
    }
}